#!/usr/bin/python
# -*- coding: utf-8 -*-  
"""
@Project : hello 
@file : db_manager.py
@Author : shenj
@time : 2025/5/28 15:20
@func :
"""
# db_manager.py

from typing import Dict
from mysql_demo import MySQLHelper
from com.cn.for_cdc.common.cdc_conmons import mysql_pro_connections


class DBManager:
    def __init__(self):
        self._connections: Dict[str, MySQLHelper] = {}

    def get_connection(self, bu: str) -> MySQLHelper:
        """根据 BU 获取对应的数据库连接，缓存避免重复创建"""
        if bu not in self._connections:
            db_config = mysql_pro_connections.get(f"{bu}_pro_job")
            if not db_config:
                raise ValueError(f"未找到 BU={bu} 的数据库配置")
            self._connections[bu] = MySQLHelper(**db_config)
        return self._connections[bu]

    def close_all(self):
        """关闭所有连接（可选）"""
        for conn in self._connections.values():
            conn.close()  # 假设 MySQLHelper 提供了 close 方法
        self._connections.clear()
